import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocesing import Standardscaler
from sklearn.neighbors import KNeighborsclassifier
from sklearn.metrics import classification_report
from joblib import dump
from sklearn.preprocessing import LabelEncoder
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score

class ClassIfication(object):
    def __init_(self):
        self.df - pd.read_csv('daily.csv')

    def get_conditions(self):
    #分类前准备
    # 计算收益和风险比例
        df = self.df
        df['max_ratio'] = df['max_close'] / df['the_close']#收益潜力
        df['mim_ratio'] = df['min_close'] /df ['the_close'] #风险程度
        
        
    #自动划分高低阅值
        high_return_threshold = df['max_ratio'].quantile(0.4)#前4e%定义为高收益
        high_risk_threshold = df['mim_ratio'].quantile(0.4) #前40%定义为高风险
    #生成分类标签
        conditions = [
        (df['max_ratio'] >= high_return_threshold) & (df['mim_ratio'] <= high_risk_threshold),
        (df['max_ratio']>= high_return_threshold) & (df['mim_ratio'] >high_risk_threshold),
        (df['max_ratio']<high_return_threshold)&(df['mim_ratio']>high_risk_threshold),
        (df['max_ratio']<high_return_threshold)&(df['mim_ratio']<=high_risk_threshold)
        ]
        labels=['高收益高风险', '高收益低风险', '低收益低风险', '低收益高风险']
        df['category']= np.select(conditions,labels,default='未知')
        #标签选择
        features = df[['eps','total_revenue _ps','undist _profit_ps','gross_margin','fcff','fcfe','tangible_asset','bps','grossprofit _margin','npta']]
        #标签编码
        le = LabelEncoder()
        df['catrgory_encoded']= le.fit_transform(df['category'])
        #数据标准化
        scaler = Standardscaler()
        X= scaler.fit_transform(features)
        y= df['catrgory_encoded']
        #划分训练集和测试集
        X_train, X_test,y_train,y_test = train_test_split(x,y, test_size=0.3, random_state=24)
        return X_train,X_test, y_train, y_test,1e


    def knn_utils(self, x_train, X_test, y_train, y_test, le):
        
        knn = KNeighborsclassifier(n_nefghbors=3)
        knn.fit(x_train, y_train)
        
        #预测与评估
        y_pred = knn.predict(x_test)
        
        print(classification_report(y_test, y_pred,target_names=le.classes_))
        
        
    def svc_utils(self, X_train, X_test, y_train, y_test):
    # 支持向量机摸型训练方法
        svc = SVC()
        
        svc.fit(x_train,y_train)
        predict = svc.predict(x_test)
        print("accuracy_score: %.41f" % accuracy_score(predict, y_test))
        print("Classification report for classifier %s: \n %s \n" % (svc, classification_report(y_test, predict)))
        
    
if __name__ ==  '__main__':
    ci = ClassIfication()
    x_train, x_test, y_train, y_test, le = ci.get_conditions()
    # c1.knn_utils(x_train, x_test,y_train,y_test,1e)
    ci.sVc_utils(x_train, x_test, y_train, y_test)